package org.irri.smta.ui.converter;

import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.inject.Named;

import org.irri.smta.domain.Role;

@Named("roleConverter")
@SessionScoped
public class RoleConverter extends ConverterBase {

	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String value) {
		if (value != null && value.length() > 0) {
			Long id = Long.parseLong(value);
			for (Role role : getGlobalBean().getRoleList()) {
				if (role.getId().longValue() == id.longValue()) {
					return role;
				}
			}
			return null;
		}
		return null;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value) {
		if (value != null) {
			return value.toString();
		}
		return null;
	}

}
